//
//  BQShopingCarView.m
//  BeeQuick
//
//  Created by AaronWei on 2017/7/11.
//  Copyright © 2017年 BecomeRich. All rights reserved.
//

#import "BQShopingCarView.h"
#import "BQProductModel.h"
#import "BQShopModelArrayManager.h"

@interface BQShopingCarView ()
@property (weak, nonatomic) IBOutlet UILabel *countLabel;

@property (weak, nonatomic) IBOutlet UIButton *redCountBtn;

@property (weak, nonatomic) IBOutlet UIButton *shopBtn;

@property (weak, nonatomic) IBOutlet UIButton *increaseButton;

@property (weak, nonatomic) IBOutlet UIButton *reduceButton;

@end

@implementation BQShopingCarView

+ (instancetype)shopingCarView {
    
    return [[NSBundle mainBundle] loadNibNamed:@"BQShopingCarView" owner:nil options:nil].lastObject;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (void)setRedCount:(NSInteger)redCount {
    
    _redCount = redCount;
    
    _redCountBtn.hidden = !redCount;
    
    [_redCountBtn setTitle:[NSString stringWithFormat:@"%zd", redCount] forState:UIControlStateNormal];
    
}

- (void)setModel:(BQProductModel *)model {
    
    _model = model;
    
    self.countLabel.text = [NSString stringWithFormat:@"%zd", self.model.count];
    
}

- (IBAction)clickedCountChangeButton:(id)sender {
    
    if (sender == _increaseButton) {
        
        if (self.model.count >= self.model.number ) {
            
            NSString * str = [self.model.name stringByAppendingString:@"库存不足了,请等待货源充足再购买~"];
            
            [SVProgressHUD showImage:[UIImage imageNamed:@"v2_orderSuccess"] status:str];
            
            [SVProgressHUD dismissWithDelay:2.0f];
            
            return;
        }
        
        [[BQShopModelArrayManager manager] shopModelArrayManagerAddWithModel:self.model];
        
        self.model.count++;
        
    } else {
        
        [[BQShopModelArrayManager manager] shopModelArrayManagerReduceWithModel:self.model];
        
        self.model.count > 0 ? self.model.count-- : self.model.count;
        
    }
    
    self.countLabel.text = [NSString stringWithFormat:@"%zd", self.model.count];
    
    self.redCount = [[BQShopModelArrayManager manager] shopManagerShowModelCount];

    [UIView animateWithDuration:.1 animations:^{
        self.redCountBtn.transform = CGAffineTransformMakeScale(1.5, 1.5);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:.1 animations:^{
            self.redCountBtn.transform = CGAffineTransformIdentity;
        }];
    }];
    
    [[BQShopModelArrayManager manager] tabBarShowCount];

}


- (IBAction)actionShopBtn:(id)sender {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:ToClickShopBtnNotificationName object:self.shopBtn];
}

@end
